import { doAjax } from "utils";

const state = {
  base: {
    page: {
      total: '', // 总条数
      per_page: '', // 每页条数,
      current_page: 0, // 当前页,
      last_page: '', // 最后一页页码
    }
  },
  data: {
    map: {},
    summary: {},
    detail: []
  }
}

// getters
const getters = {

}

// actions
const actions = {
  getMap({ commit }) {
    let self = this;
    return doAjax({
      url: URLConfig('messageMap'),
      sucCallback(data) {
        commit('UPDATE_MAP', data);
      }
    });  
  },
  getMessageSummary({ commit }, params) {
    // console.log('getMessageSummary', params);
    return doAjax({
      url: URLConfig('messageSummary'),
      data: params,
      sucCallback(data) {
        commit('UPDATE_SUMMARY', data);
      }
    });
  },
  getMessageList({ commit }, params) {
    return doAjax({
      url: URLConfig('messageList'),
      data: params,
      sucCallback(data) {
        commit('UPDATE_PAGE', {
          total: data.total,
          per_page: data.per_page,
          current_page: data.current_page,
          last_page: data.last_page
        });
        commit('UPDATE_DETAIL', data.data);
      }
    });
  },
  checkMessage({ commit }, params) {
    return doAjax({
      url: URLConfig('messageCheck'),
      method: 'post',
      data: params,
      sucCallback(data) {
      }
    });
  },
  sendMessage({ commit }, params) {
    return doAjax({
      url: URLConfig('messageSend'),
      method: 'post',
      data: params,
      sucCallback(data) {
      }
    });
  },
  deleteMessage({ commit }, params) {
    return doAjax({
      url: URLConfig('messageDelete'),
      data: params,
      sucCallback(data) {
      }
    });
  }
}

// mutations
const mutations = {
  UPDATE_MAP(state, data) {
    state.data.map = data;
  },
  UPDATE_SUMMARY(state, data) {
    // console.log('summary', data);
    state.data.summary = data;
  },
  UPDATE_DETAIL(state, data) {
    state.base.page.current_page == 1
      ? state.data.detail = data
      : state.data.detail = state.data.detail.concat(data);
  },
  UPDATE_PAGE(state, data) {
    state.base.page = data;
    // console.log('detail', data);
  }
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}